/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mindbox.mueblesalpes.presentacion.utils;

import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet para procesar las imagenes.
 * @author jsanchez
 */
public class ServletImage extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.reset();
        response.setContentType("image/jpg");
        try {
            String foto = request.getParameter("image");
            if(foto != null ) {
                BeanImage bean = UtilidadesImages.getImage(getServletContext(), foto);
                if(bean != null){
                    byte imageBytes[] = bean.getImageBytes();

                    int buffer = 1024;
                    response.setBufferSize(buffer);
                    int longitud = imageBytes.length;
                    int ciclos = longitud / buffer;
                    int resto = longitud % buffer;
                    int i = 0;
                    int termino = 0;

                    for( ; i < ciclos ; i++){
                        termino = ((buffer*(i+1)));
                        byte copy[] = Arrays.copyOfRange(imageBytes, i*buffer, termino);
                        response.getOutputStream().write( copy );
                        response.getOutputStream().flush();
                    }
                    if(resto > 0){
                        byte copy[] = Arrays.copyOfRange(imageBytes, termino, (termino + resto) );
                        response.getOutputStream().write( copy );
                        response.getOutputStream().flush();
                }
                }
                else
                    response.getOutputStream().write(new byte[0]);
            }
        } finally { 
            response.getOutputStream().close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
